define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'saler/groupinfo/index',
                    add_url: 'saler/groupinfo/add?saler_group_id='+$('#saler_group_id').text(),
                    // edit_url: 'saler/groupinfo/edit?saler_group_id='+$('#saler_group_id').text(),
                    del_url: 'saler/groupinfo/del?saler_group_id='+$('#saler_group_id').text(),
                    multi_url: 'saler/groupinfo/multi',
                    import_url: 'saler/groupinfo/import?saler_group_id=' + $('#saler_group_id').text(),
                    table: 'saler_group_info',
                }
            });

            var table = $("#table");
            var imports_id = $('#saler_group_id').text();
            var getdata;
            var array=[];
            $.ajax({
                cache: false,
                async: false,
                type: 'get',
                url: "/admin/saler/groupinfo/groupDetail?saler_group_id="+imports_id + "",
                success: function (data) {
                    if(data.tts_format.length > 0){
                        getdata=JSON.parse(data.tts_format);
                    }
                }
            });
             for (var key in getdata){
                array.push(key);
            }
            // console.log('saler/groupinfo/import?saler_group_id='+$('#saler_group_id').text());
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize: 20,
                pageList: [20, 50],
                clickToSelect: false,
                dblClickToEdit: false,
                singleSelect: false,
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id'),
                            visible: false
                        },
                        {
                            field: 'saler_id',
                            title: __('Saler_id'),
                            visible: false
                        },
                        {
                            field: 'saler_group_id',
                            title: __('Saler_group_id'),
                            visible: false
                        },
                        {
                            field: 'salergroup.name',
                            title: __('Salergroup.name')
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime,
                            visible: false
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime,
                            visible: false
                        },
                        {
                            field: 'saler.id',
                            title: __('Saler.id'),
                            visible: false
                        },
                        {
                            field: 'saler.admin_id',
                            title: __('Saler.admin_id'),
                            visible: false
                        },
                        {
                            field: 'saler.name',
                            title: __('Saler.name')
                        },
                        {
                            field: 'saler.tel',
                            title: __('Saler.tel')
                        },
                        {
                            field: 'saler.company',
                            title: __('公司名称')
                        },
                        {
                            field: 'salergroup.id',
                            title: __('Salergroup.id'),
                            visible: false
                        },
                        {
                            field: 'json.'+array[0]+'',
                            title: array[0],
                            visible: visible(1,array)
                        },
                        {
                            field: 'json.'+array[1]+'',
                            title: array[1],
                            visible: visible(2,array)
                        },
                        {
                            field: 'json.'+array[2]+'',
                            title:array[2],
                            visible: visible(3,array)
                        },
                        {
                            field: 'json.'+array[3]+'',
                            title: array[3],
                            visible: visible(4,array)
                        },
                        {
                            field: 'json.'+array[4]+'',
                            title: array[4],
                            visible: visible(5,array)
                        },
                        {
                            field: 'json.'+array[5]+'',
                            title: array[5],
                            visible: visible(6,array)
                        },
                        {
                            field: 'json.'+array[6]+'',
                            title: array[6],
                            visible: visible(7,array)
                        },
                        {
                            field: 'json.'+array[7]+'',
                            title: array[7],
                            visible: visible(8,array)
                        },
                        {
                            field: 'json.'+array[8]+'',
                            title:array[8],
                            visible: visible(9,array)
                        },
                        {
                            field: 'json.'+array[9]+'',
                            title:array[9],
                            visible: visible(10,array)
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            function visible(i,lg){
                var count=lg.length;
                if(i<=count){
                    return true;
                }else{
                    return false;
                }
            }
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});